//
//  DetialBaseViewController.m
//  Wedding fantastic
//
//  Created by ZLM on 15/6/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DetialBaseViewController.h"

@interface DetialBaseViewController ()

@end

@implementation DetialBaseViewController
-(void)dealloc{
    [_VC1 release];
    [_VC2 release];
    [_VC3 release];
    [_buttonName release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor clearColor];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
    UIImage *aimage = [UIImage imageNamed:@"iconfont-yingjirepi"];
    UIImage *bimage = [aimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:bimage style:UIBarButtonItemStylePlain target:self
    action:@selector(presentLeftMenuViewController:)];
//  创建套餐视图控制器
    self.VC1 = [[TaoCanViewController alloc]init];
//    创建案例视图控制器
    self.VC2 = [[AnLiViewController alloc]init];
//    创建商家视图控制器
    self.VC3 = [[ShangJiaViewController alloc]init];
//    添加子视图
    [self addChildViewController:self.VC1];
    [self addChildViewController:self.VC2];
    [self addChildViewController:self.VC3];
    [self.view addSubview:self.VC1.view];
    [self.view addSubview:self.VC2.view];
    [self.view addSubview:self.VC3.view];
    [self.VC1 release];
    [self.VC2 release];
    [self.VC3 release];
//   套餐，案例，商家分段按钮
//    UISegmentedControl *segmentedControl = [ [ UISegmentedControl alloc ]initWithItems: nil ];
//    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
//    [ segmentedControl insertSegmentWithTitle:
//     @"套餐" atIndex: 0 animated:YES];
//    [ segmentedControl insertSegmentWithTitle:
//     @"案例" atIndex: 1 animated:YES];
//    [segmentedControl insertSegmentWithTitle:
//     @"商家" atIndex:2 animated:YES];
   
    // 第三方segmentedControl
    CCSegmentedControl *listSegmented = [[CCSegmentedControl alloc] initWithItems:@[@"套餐", @"案例", @"商家"]];
    listSegmented.frame = CGRectMake(0, 64, 375 * kWidth / 375, 50 * kHeight / 667);
    listSegmented.layer.borderWidth = 0.5;
    listSegmented.layer.borderColor = [[UIColor orangeColor] CGColor];
    //设置背景图片，或者设置颜色，或者使用默认白色外观
    //    listSegmented.backgroundImage = [UIImage imageNamed:@"segment_bg.png"];
    listSegmented.backgroundColor = [UIColor whiteColor];
    
    //阴影部分图片，不设置使用默认椭圆外观的stain
    listSegmented.selectedStainView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"stain.png"]];
    listSegmented.selectedStainView = [[UIImageView alloc] init];
    listSegmented.selectedSegmentTextColor = [UIColor brownColor];
    listSegmented.segmentTextColor = [self colorWithHexString:@"#535353"];
    [listSegmented addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:listSegmented];
    [listSegmented release];
    
//    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

    //    把分段按钮添加到导航栏标题视图上
//    self.navigationItem.titleView = segmentedControl;
//  设置分段按钮选中项为套餐
    listSegmented.selectedSegmentIndex = 0;
//  给套餐视图控制器按钮名称属性赋值，用于判断上一页面点击的哪个按钮
    self.VC1.myButtonName = self.buttonName;
//    将套餐视图控制器置顶
    [self.view bringSubviewToFront:self.VC1.view];
    [self.view bringSubviewToFront:listSegmented];
}
//分段按钮方法
//-(void)segmentAction:(UISegmentedControl *)segmentButton{
////套餐视图控制器置顶
//    if (segmentButton.selectedSegmentIndex == 0) {
//        self.VC1.myButtonName = self.buttonName;
//        NSLog(@"~~~~~3333 %@",self.VC1.myButtonName);
//        [self.view bringSubviewToFront:self.VC1.view];
//    }
////    案例视图控制器置顶
//    if (segmentButton.selectedSegmentIndex == 1) {
//        self.VC2.myButtonName = self.buttonName;
//        [self.view bringSubviewToFront:self.VC2.view];
//    }
////    商家视图控制器置顶
//    if (segmentButton.selectedSegmentIndex == 2) {
//        self.VC3.myButtonName = self.buttonName;
//        [self.view bringSubviewToFront:self.VC3.view];
//    }
//    
//
//}

- (UIColor *) colorWithHexString: (NSString *) hexString {
    NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
    CGFloat alpha, red, blue, green;
    switch ([colorString length]) {
        case 3: // #RGB
            alpha = 1.0f;
            red   = [self colorComponentFrom: colorString start: 0 length: 1];
            green = [self colorComponentFrom: colorString start: 1 length: 1];
            blue  = [self colorComponentFrom: colorString start: 2 length: 1];
            break;
        case 4: // #ARGB
            alpha = [self colorComponentFrom: colorString start: 0 length: 1];
            red   = [self colorComponentFrom: colorString start: 1 length: 1];
            green = [self colorComponentFrom: colorString start: 2 length: 1];
            blue  = [self colorComponentFrom: colorString start: 3 length: 1];
            break;
        case 6: // #RRGGBB
            alpha = 1.0f;
            red   = [self colorComponentFrom: colorString start: 0 length: 2];
            green = [self colorComponentFrom: colorString start: 2 length: 2];
            blue  = [self colorComponentFrom: colorString start: 4 length: 2];
            break;
        case 8: // #AARRGGBB
            alpha = [self colorComponentFrom: colorString start: 0 length: 2];
            red   = [self colorComponentFrom: colorString start: 2 length: 2];
            green = [self colorComponentFrom: colorString start: 4 length: 2];
            blue  = [self colorComponentFrom: colorString start: 6 length: 2];
            break;
        default:
            [NSException raise:@"Invalid color value" format: @"Color value %@ is invalid.  It should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB", hexString];
            break;
    }
    return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}

-(void)valueChange:(id)sender
{
    CCSegmentedControl* segmentedControl = sender;
    switch (segmentedControl.selectedSegmentIndex) {
        case 0:
        {
            self.VC1.myButtonName = self.buttonName;
            
                    NSLog(@"~~~~~3333 %@",self.VC1.myButtonName);
                    [self.view bringSubviewToFront:self.VC1.view];
            [self.view bringSubviewToFront:segmentedControl];
        }
            break;
        case 1:
        {
            self.VC2.myButtonName = self.buttonName;
                    [self.view bringSubviewToFront:self.VC2.view];
            [self.view bringSubviewToFront:segmentedControl];
        }
            break;
        case 2:
        {
            self.VC3.myButtonName = self.buttonName;
                    [self.view bringSubviewToFront:self.VC3.view];

            [self.view bringSubviewToFront:segmentedControl];
        }
            break;
        default:
            break;
    }
    
}
- (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length {
    NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
    NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
    unsigned hexComponent;
    [[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
    return hexComponent / 255.0;
}

//-(void)rightButtonAction:(UIButton *)button{
//    
//    [self.navigationController popViewControllerAnimated:YES];
//
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
